package com.blb.thread;

import java.util.concurrent.CountDownLatch;

/**
 * 使用CountDownLatch
 */
public class CountDownLatchDemo {

    public static void main(String[] args) {
        //定义CountDownLatch对象
        CountDownLatch latch = new CountDownLatch(3);
        Thread thread = new Thread(() ->{
            System.out.println(Thread.currentThread().getName()+"准备好了！"+latch.getCount());
            try {
                //阻塞当前线程
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"冲刺了，到达终点！");
        });
        thread.setName("闪电");
        thread.start();

        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                //倒计时一次
                latch.countDown();
                System.out.println(Thread.currentThread().getName()+"倒计时完成！"+latch.getCount());
            }).start();
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
